<template>
	<view>
		<view class="content">
			<view class="row">
				<view class="nominal">
					收件人
				</view>
				<view class="input">
					<input placeholder="请输入收件人姓名" type="text" v-model="consignee" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					电话号码
				</view>
				<view class="input">
					<input placeholder="请输入收件人电话号码" type="text" v-model="mobile" />
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					所在地区
				</view>
				<view class="input" @tap="chooseCity">
					{{region}}
				</view>
				
			</view>
			<view class="row">
				<view class="nominal">
					详细地址
				</view>
				<view class="input">
					<textarea v-model="address" auto-height="true" placeholder="输入详细地址"></textarea>
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					设置默认地址
				</view>
				<view class="input switch">
					<switch style="transform: scale(0.6)" color="#E80505" :checked="isnoDefault === '1'" @change=isDefaultChange />
				</view>
			</view>
			<view class="row" v-if="editType=='edit'" @tap="del">
				<view class="del">
					删除收货地址
				</view>
			</view>
		</view>
		<view class="save" @tap="save">
			<view class="btn">
				保存地址
			</view>
		</view>
		<mpvue-city-picker :themeColor="themeColor" ref="mpvueCityPicker" :pickerValueDefault="cityPickerValue" @onCancel="onCancel" @onConfirm="onConfirm"></mpvue-city-picker>
	</view>
</template>

<script>
	import {  
	    mapState,
		mapActions
	} from 'vuex';  
	import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue'
	import addressApis from'@/common/services/address.js'
	export default {
		components: {
			mpvueCityPicker
		},
		data() {
			return {
				editType:'edit',
				id:'', // 地址id
				consignee:'', // 收货人
				mobile:'', // 手机号码
				province: '', // 省级编码
				city: '', // 市级编码
				street: '', // 街道编码
				district: '',  // 县级编码
				address:'', // 详细地址
				isnoDefault: "0", // 是否默认
				cityPickerValue: [0, 0, 0,0],
				themeColor: '#007AFF',
				region: "请点击选择地址"
			};
		},
		computed: { 
		    ...mapState(['addressList']),
			allAddress: function() { // 完整地址
				return this.region + this.address
			}
		},
		methods: {
			onCancel(e) {
				console.log(e)
			},
			chooseCity() {
				this.$refs.mpvueCityPicker.show()
			},
			onConfirm(e) {
				console.log('ssss:', e)
				this.region = e.label;
				this.city = e.city;
				this.province = e.province;
				this.district = e.district;
				this.street = e.street;
			},
			isDefaultChange(e){
				this.isnoDefault = e.detail.value ? "1" : "0";
			},
			del(){
				uni.showModal({
					title: '删除提示',
					content: '你将删除这个收货地址',
					success: (res)=>{
						if (res.confirm) {
							addressApis.delAddress(this.id).then(res => {
								this.removeAddress({id: this.id})
								uni.navigateBack();
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			save(){
				let data={
					"consignee":this.consignee,
					// "head":this.consignee.substr(0,1),
					"mobile":this.mobile,
					"address":this.address,
					"isnoDefault":this.isnoDefault, 
					"province": this.province,
					"city": this.city,
					"street": this.street,
					"district": this.district,
					"allAddress": this.allAddress
				}
				if(this.editType=='edit'){
					data.id = this.id
				}
				if(!data.consignee){
					uni.showToast({title:'请输入收件人姓名',icon:'none'});
					return ;
				}
				if(!data.mobile){
					uni.showToast({title:'请输入收件人电话号码',icon:'none'});
					return;
				}
				if (!this.$validate.tel(data.mobile)) {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号格式错误'
				    });
				    return;
				}
				if(!data.address){
					uni.showToast({title:'请输入收件人详细地址',icon:'none'});
					return ;
				}
				if(!data.province){
					uni.showToast({title:'请选择收件地址',icon:'none'});
					return ;
				}
				uni.showLoading({
					title:'正在提交'
				})
				addressApis.addAddress(data).then(res => {
					if (this.editType=='edit') {
						this.updateAddress({id: this.id, address: data})
					} else {
						// 需要发送一下请求，拿到新的地址id
						this.addAddress({ address: {...data}})
					}
					//实际应用中请提交ajax,模板定时器模拟提交效果
					setTimeout(()=>{
						uni.hideLoading();
						uni.navigateBack();
					},300)
				})
			},
			...mapActions({
				removeAddress: 'REMOVE_ADDRESS',
				updateAddress: 'UPDATE_ADDRESS',
				addAddress: 'ADD_ADDRESS',
				setAddressList: 'SET_ADDRESS_LIST',
			})
		},
		onLoad(e) {
			addressApis.getAllAddress().then(res => {
				this.addressList = res.records;
				this.setAddressList({ address: res.records})
				//获取传递过来的参数
				this.editType = e.type;
				if(e.type=='edit'){
					const address = this.addressList.find(item => item.id === e.id);
					this.id = address.id;
					this.consignee = address.consignee;
					this.mobile = address.mobile;
					this.address = address.address;
					this.isnoDefault = address.isnoDefault;
					this.allAddress = address.allAddress;
					this.region = address.allAddress.split(address.address)[0];
					this.city = address.city;
					this.province = address.province;
					this.district = address.district;
					this.street = address.street;
				}
			})	
		},
		onBackPress() {
			if (this.$refs.mpvueCityPicker.showPicker) {
				this.$refs.mpvueCityPicker.pickerCancel();
				return true;
			}
		},
		onUnload() {
			if (this.$refs.mpvueCityPicker.showPicker) {
				this.$refs.mpvueCityPicker.pickerCancel()
			}
		}
	};
</script>
<style lang="scss">

.save{
		view{
			display: flex;
		}
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120upx;
		display: flex;
		justify-content: center;
		align-items: center;
		.btn{
			box-shadow: 0upx 5upx 10upx rgba(0,0,0,0.4);
			width: 70%;
			height: 80upx;
			border-radius: 80upx;
			background-image:linear-gradient( 135deg, #FDD819 10%, #E80505 100%);
			color: #fff;
			justify-content: center;
			align-items: center;
			.icon{
				height: 80upx;
				color: #fff;
				font-size: 30upx;
				justify-content: center;
				align-items: center;
			}
			font-size: 30upx;
		}
	}
	.content{
		display: flex;
		flex-wrap: wrap;
		view{
			display: flex;
		}
		.row{
			width: 94%;
			
			margin: 0 3%;
			border-top: solid 1upx #eee;
			.nominal{
				width: 30%;
				height: 120upx;
				font-weight: 200;
				font-size: 30upx;
				align-items: center;
			}
			.input{
				width: 70%;
				padding: 20upx 0;
				align-items: center;
				font-size: 30upx;
				&.switch{
					justify-content: flex-end;
				}
				.textarea{
					margin: 20upx 0;
					min-height: 120upx;
				}
			}
			.del{
				width: 100%;
				height: 100upx;
				justify-content: center;
				align-items: center;
				font-size: 36upx;
				color:linear-gradient( 135deg, #FDD819 10%, #E80505 100%);
				background-color: rgba(255,0,0,0.05);
				border-bottom: solid 1upx #eee;
			}
		}
	}
	
</style>
